// 使用命名管道，A为写端，写 i am process A，并且由B读到打印在标准输出

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstring>
#include <fcntl.h>
#include <cstdio>
#include <unistd.h>

#include "common.hpp"

int main()
{
    // 创建命名管道
    int n = mkfifo(pathname.c_str(), 0666);
    if (n < 0)
    {
        ERR_EXIT("mkfifo");
    }
    std::cout << "mkfifo success" << std::endl;

    // 打开命名管道准备写
    int fd = open(pathname.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if(fd < 0)
    {
        ERR_EXIT("open");
    }
    std::cout << "open success" << std::endl;

    char buffer[1024];
    snprintf(buffer, sizeof buffer - 1, "i am process A");
    int m = write(fd, buffer, strlen(buffer));
    if(m < 0)
    {
        ERR_EXIT("write");
    }
    std::cout << "write success" << std::endl;

    // 写完之后关闭管道文件并且删除
    close(fd);
    unlink(pathname.c_str());

    return 0;
}